package com.reggie.console.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.reggie.acl.dataobject.Dish;
import com.reggie.console.annotation.AuthRule;
import com.reggie.console.dto.ResultDTO;
import com.reggie.console.enums.ReggieRole;
import com.reggie.console.util.ResultBuilder;
import com.reggie.service.model.bo.DishBO;
import com.reggie.service.model.request.DishPageRequest;
import com.reggie.service.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author yth
 * @since 2023-02-04
 */
@Slf4j
@RestController
@RequestMapping("/dish")
@AuthRule(roles = {ReggieRole.EMPLOYEE})
public class DishController {

    private static final String EXPIRE_TIME = "1920";   // 32 MIN

    @Autowired
    private DishService dishService;

    /**
     * 根据菜品名称，分页查询
     *
     * @param page     当前页码
     * @param pageSize 每页数据量
     * @param name     菜品名称
     * @return 数据
     */
    @GetMapping("/page")
    @AuthRule(roles = {ReggieRole.CUSTOMER})
    public ResultDTO<IPage<DishBO>> page(int page, int pageSize, String name) {
        DishPageRequest request = new DishPageRequest();
        request.setPage(page);
        request.setPageSize(pageSize);
        request.setName(name);
        IPage<DishBO> pageInfo = dishService.page(request);
        return ResultBuilder.ok(pageInfo);
    }

    /**
     * 根据id，批量删除/单个删除<p>
     * 只能删除停售的套餐
     *
     * @param ids 删除id集合
     * @return 消息
     */
    @CacheEvict(value = "dishCache", allEntries = true)
    @DeleteMapping
    public ResultDTO<String> remove(@RequestParam List<Long> ids) {
        dishService.removeBatchIds(ids);
        return ResultBuilder.ok("菜品删除成功");
    }

    /**
     * 批量更新菜品状态
     *
     * @param status 状态：0 停售 1 起售
     * @param ids    id集合
     * @return 消息
     */
    @CacheEvict(value = "dishCache", allEntries = true)
    @PostMapping("/status/{status}")
    public ResultDTO<String> status(@PathVariable int status, @RequestParam List<Long> ids) {
        log.info("status()");
        dishService.updateStatusBatchIds(status, ids);
        return ResultBuilder.ok("菜品状态更新成功");
    }

    /**
     * 新增菜品
     *
     * @param dishBO 菜品数据
     * @return 消息
     */
    @CacheEvict(value = "dishCache", allEntries = true)
    @PostMapping
    public ResultDTO<String> save(@RequestBody DishBO dishBO) {
        log.info("save()");
        dishService.saveWithFlavor(dishBO);
        return ResultBuilder.ok("菜品添加成功");
    }

    /**
     * 根据菜品id，查询菜品<p>
     * 携带菜品口味数据
     *
     * @param id 菜品id
     * @return 数据
     */
    @GetMapping("/{id}")
    public ResultDTO<DishBO> getById(@PathVariable long id) {
        log.info("getById()");
        DishBO dishBO = dishService.getWithFlavorById(id);
        return ResultBuilder.ok(dishBO);
    }

    /**
     * 修改菜品（菜品口味）
     *
     * @param dishBO 菜品数据
     * @return 消息
     */
    @CacheEvict(value = "dishCache", allEntries = true)
    @PutMapping
    public ResultDTO<String> update(@RequestBody DishBO dishBO) {
        log.info("update()");
        dishService.update(dishBO);
        return ResultBuilder.ok("修改菜品成功");
    }

    /**
     * 根据菜品分类id、菜品状态、菜品名称，查询菜品集合
     *
     * @param dish 查询条件
     * @return 数据
     */
    @Cacheable(value = "dishCache#" + EXPIRE_TIME, sync = true,
            key = "#dish.categoryId + '_' + #dish.status + '_' + #dish.name")
    @GetMapping("/list")
    @AuthRule(roles = {ReggieRole.CUSTOMER})
    public ResultDTO<List<DishBO>> list(Dish dish) {
        log.info("list()");
        List<DishBO> dishBOList = dishService.listWithFlavor(dish);
        return ResultBuilder.ok(dishBOList);
    }
}

